﻿using NewLife;

namespace NetServer
{
    public class FileOperation
    {
        /// <summary>
        /// 添加共享文件夹
        /// </summary>
        /// <param name="sourceFolder">当前目录</param>
        /// <param name="fileName">创建文件名</param>
        /// <param name="isDelete">是否删除</param>
        /// <returns></returns>
        /// <exception cref="CustomException"></exception>
        public static bool CreateFile(string sourceFolder, string fileName, bool isDelete)
        {
            try
            {
                var dicInfo = new DirectoryInfo(sourceFolder); // 选择的目录信息

                FileSystemInfo[] fileinfo = dicInfo.GetFileSystemInfos();

                if (IsExistFile(fileinfo, fileName))
                {
                    if (isDelete)
                    {
                        Directory.Delete(@$"{sourceFolder}\{fileName}", true);
                    }
                    else
                    {
                        return false;
                    }
                }

                //创建下级目录
                dicInfo.CreateSubdirectory(fileName);
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("创建文件失败", ex);
            }
        }

        public static void CreateFile(string filePath)
        {
            try
            {
                // 创建文件
                using (FileStream fs = File.Create(filePath))
                {
                    Console.WriteLine("创建完成！");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error creating file: " + ex.Message);
            }
        }

        public static void WriteData(string filePath, string data)
        {
            try
            {
                // 写入数据
                using (StreamWriter sw = File.AppendText(filePath))
                {
                    // 使用正则表达式来匹配换行符和空格
                    //Regex regex = new Regex(@"\s*");

                    // 替换匹配的内容为空字符串
                    //data = regex.Replace(data, "");

                    sw.WriteLine(data.TrimEnd(" "));
                    //Console.Write("添加数据成功：" + data);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error writing data to the file: " + ex.Message);
            }
        }

        public static void AppendData(string filePath, string data)
        {
            try
            {
                // 追加数据
                using (StreamWriter sw = File.AppendText(filePath))
                {
                    sw.WriteLine(data);
                    Console.WriteLine("Data appended to the file successfully.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error appending data to the file: " + ex.Message);
            }
        }

        /// <summary>
        /// 是否存在共享文件夹
        /// </summary>
        /// <param name="fileinfo"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool IsExistFile(FileSystemInfo[] fileinfo, string fileName)
        {
            bool isExist = false;
            List<string> list = new List<string>();

            foreach (FileSystemInfo i in fileinfo)
            {
                if (i is DirectoryInfo)
                {
                    string name = i.Name;

                    list.Add(name);
                }
            }

            if (list.Contains(fileName))
            {
                isExist = true;
            }

            return isExist;
        }
    }
}